<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\service\Invoice as InvoiceService;
use think\Request;

class Invoice extends Base
{

    protected $request;
    protected $service;
    protected $validate;
    protected $operator_id;

    public function __construct(Request $request,  InvoiceService $service)
    {
        $this->request = $request;
        $this->service = $service;
        $this->validate = new \app\admin\validate\Invoice();
        $this->operator_id = $request->operator_id;
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $params = empty($this->request->params) ? [] : $this->request->params;

        $pageIndex         = isset($params['pageIndex'])             ? intval($params['pageIndex'])             : 1;
        $pageSize          = isset($params['pageSize'])              ? intval($params['pageSize'])              : 1000;
        $customerId        = isset($params['customer_id'])           ? trim($params['customer_id']) : null;
        $applicant         = isset($params['applicant'])             ? trim($params['applicant']) : null;
        $orderId           = isset($params['order_id'])              ? intval($params['order_id']) : null;
        $invoice_no        = isset($params['invoice_no'])            ? trim($params['invoice_no']) : null;
        $status            = isset($params['status'])                ? intval($params['status']) : null;
        $start_time        = isset($params['start_time'])            ? trim($params['start_time']) : null;
        $end_time          = isset($params['end_time'])              ? trim($params['end_time']) : null;
        $orderNo           = isset($params['order_no'])              ? trim($params['order_no']) : null;



        $where  = ['is_del' => 0]; // $where = [];
        if ($orderNo) {
            if (!$orderType) {
                return $this->returnJsonData(100001, null, '参数错误，搜索订单编码需先确定订单类型');
            }
            $where['order_no'] = $orderNo;
        }
        if ($customerId) {
            $where[] = ['customer_id', 'in', $customerId];  // customer_id 在给定数组中
        }

        if ($applicant) {
            $where['applicant'] = ['in', $applicant];  // applicant 在给定数组中
        }

        if ($orderId) {
            $where['order_id'] = $orderId;  // order_id 等于指定值
        }


        if ($invoice_no) {
            $where[] = ['invoice_no', 'like', "%{$invoice_no}%"];  // invoice_no 模糊匹配
        }

        if ($status) {
            $where['status'] = $status;  // status 等于指定值
        }

        if ($start_time && $end_time) {
            $where[] = ['invoicing_date', 'between', [$start_time, $end_time]];  // invoicing_date 在指定时间区间内
        }
        $field = '*';
        $res =  $this->service->getList($pageIndex, $pageSize, $where, $field, $this->operator_id);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }


    /**
     * 显示创建资源表单页.
     * @author: Bobo
     * @date: 2025-02-07 16:51:40
     * @return \think\Response
     */
    public function create()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('create')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }

        $params['create_time'] = date('Y-m-d H:i:s', time());
        $params['creator'] = $this->operator_id;


        $res  = $this->service->create($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 发票撤销
     * @author: Bobo
     * @date: 2025-02-07 16:51:40
     * @return \think\Response
     */
    public function cancel()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('cancel')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }


        $params['operator_id'] = $this->operator_id;


        $res  = $this->service->cancel($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * 显示创建资源表单页.
     * @author: Bobo
     * @date: 2025-02-07 16:51:40
     * @return \think\Response
     */
    public function check()
    {
        $params = $this->request->params;

        $params['update_time'] = date('Y-m-d H:i:s', time());
        $params['operator_id'] = $this->operator_id;

        $res  = $this->service->check($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $id = intval($id);
        if (!$id) {
            return $this->returnJsonData(100001, null, 'id不能为空');
        }
        $res = $this->service->getOne($id);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }



    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $params['updater'] = $this->operator_id;
        $params['update_time'] = date('Y-m-d H:i:s', time());

        $where = [];
        if ($params['id']) {
            $where['id'] = intval($params['id']);
        }
        $res  = $this->service->update($where, $params);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 上传发票
     * @author: Bobo
     * @date: 2025-02-14 16:26:25 
     * @param [type] @required
     * @return Renderable
     */
    public function uploadInvoice()
    {
        $params = $this->request->params;
        if (empty($params['id']) || !is_numeric($params['id']) || $params['id'] <= 0 || !isset($params['invoice_attach']) || !preg_match('/^[0-9,]+$/', $params['invoice_attach'])) {
            return $this->returnJsonData(100001, [], '参数错误');
        }

        $params['updater'] = $this->operator_id;
        $params['update_time'] = date('Y-m-d H:i:s', time());
        $params['status'] = 40;
        $where = [];
        if ($params['id']) {
            $where['id'] = intval($params['id']);
        }

        $res  = $this->service->uploadInvoice($where, $params);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('delete')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $ids =  trim($params['ids']);
        $res = $this->service->batchLogicalDelete($ids);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-11 17:02:30 
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum(Request $request)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $this->service->getFieldEnum($param);
        return json($arrData);
    }
}
